package com.vincce.sport.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.vincce.sport.core.utils.CheckUtil;
import com.vincce.sport.core.utils.SimpleStorageClient;
import com.vincce.sport.entity.Page;
import com.vincce.sport.entity.Video;
import com.vincce.sport.mapper.VideoMapper;

@Controller
@RequestMapping("/video")
public class VideoController extends BaseController{

	@Resource
	private VideoMapper videoMapper;
	
	@RequestMapping("/list")
	public String queryVideoByPage(Model model,Page<Video> page,Video video){
		Map<String, Object> filter = new HashMap<String, Object>();
		if(video.getName() != null && !video.getName().equals("")){
			filter.put("name", video.getName());
		}
		page.setFilter(filter);
		page.setTotal(videoMapper.queryByVideoCount(page.getFilter()));
		List<Video> list = videoMapper.queryByVideoPage(page);
		page.setResult(replace(list));
		Video videoMore = videoMapper.queryVideoMore();
//		model.addAttribute("zoneList",zoneMapper.queryByZoneAll());
		model.addAttribute("videoMore",videoMore==null?new Video():videoMore);
		model.addAttribute("video",video==null?new Video():video);
		model.addAttribute("page",page);
		return "/backstage/videoList";
	}
	//转换图片路径
		private List<Video> replace(List<Video> imgTempList) {
			if(imgTempList!=null && imgTempList.size()>0){
				for(Video itl : imgTempList){
					itl.setImgUrl(String.format(SimpleStorageClient.REQUEST_URI2_PREFIX+itl.getImgUrl(), "ori"));
				}
			}
			return imgTempList;
		}
	
	@RequestMapping("/addOrUpdateVideo")
	@ResponseBody
	public String addOrUpdate(HttpServletRequest request,Video video,Model model,@RequestParam(value = "file", required = false) MultipartFile file) throws Exception{
		Long count=0L;
		String msg = "";
		Map<String, Object> result = new HashMap<String, Object>();
		SimpleStorageClient.StorageResult obj = new SimpleStorageClient.StorageResult(200, "");
		 if(CheckUtil.isEmpty(video.getId())){
			 if (file != null && !file.isEmpty()) {
	            String fileName = file.getOriginalFilename();
	            String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
	            if (!fileType.equals(".jpg") && !fileType.equals(".JPG") && !fileType.equals(".png") && !fileType.equals(".PNG")) {
            		result.put("msg", "格式不正确,只支持后缀名为.jpg或者.png的图片上传。");
	                result.put("suc", false);
	            }else{
	                obj = SimpleStorageClient.upload(2,file.getInputStream(), fileType);
	                if (obj.statusCode == 200) {
	                    String path= obj.info.replaceAll("\\\\", "/");
	                    video.setImgUrl(path);
	                    video.setCreateTime(new Date());
	                    video.setUpdateTime(new Date());
	                    video.setCreateUser(getCurrentUser().getId().intValue());
	                    video.setUpdateUser(getCurrentUser().getId().intValue());
	                    count=videoMapper.insertVideo(video);
	                }
	            }
	        } else{
	        	result.put("msg","请选择文件!");
	            result.put("suc", false);
	        }
		 }else{
			 if (file != null && !file.isEmpty()) {
		            String fileName = file.getOriginalFilename();
		            String fileType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
		            if (!fileType.equals(".jpg") && !fileType.equals(".JPG") && !fileType.equals(".png") && !fileType.equals(".PNG")) {
	            		result.put("msg", "格式不正确,只支持后缀名为.jpg或者.png的图片上传。");
		                result.put("suc", false);
		            }else{
		                obj = SimpleStorageClient.upload(2,file.getInputStream(), fileType);
		                if (obj.statusCode == 200) {
		                    String path= obj.info.replaceAll("\\\\", "/");
		                    video.setImgUrl(path);
		                    video.setUpdateTime(new Date());
		                    video.setUpdateUser(getCurrentUser().getId().intValue());
		                    count=videoMapper.updateVideo(video);
		                } 
		            }
		     }else {
                	video.setUpdateTime(new Date());
                	video.setUpdateUser(getCurrentUser().getId().intValue());
                	count = videoMapper.updateVideo(video);
             }
		 }
		 	result.put("msg", msg);
	        result.put("suc", count > 0 ? true : false);
	        return JSON.toJSONString(count > 0 ? true : false);
	}
	
	@RequestMapping("/deleteById")
	@ResponseBody
	public String deleteById(String id){
		Boolean flag=false;
		Integer count=videoMapper.deleteById(Integer.parseInt(id));
		flag=count>0?true:false;
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("suc", flag);
		return JSON.toJSONString(result);
	}
	
	@RequestMapping("/updateVideoMore")
	@ResponseBody
	public String updateVideoMore(HttpServletRequest request,String videoMore){
		long count = videoMapper.updateVideoMore(videoMore);
		Boolean flag = count >0 ? true :false;
		return JSON.toJSONString(flag);
	}
}
